﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BonSaiTableAdapters;
using Telerik.Web.UI;

public partial class Product_RepresentImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            RadUpload1.AllowedFileExtensions = new string[] { ".JPG", ".PNG" };
            if(Request.QueryString["productID"] != null)
            {
                try
                {
                    int productID = Convert.ToInt32(Request.QueryString["productID"].ToString());
                    ViewState["productID"] = productID;
                    FillImage();
                }
                catch (Exception ex) 
                {
                    Response.Redirect("ProductManage.aspx");
                }
            }
            else
            {
                Response.Redirect("ProductManage.aspx");
            }
        }
    }

    private void FillImage() 
    {
        if (ViewState["productID"] != null) 
        {
            int productID = Int32.Parse(ViewState["productID"].ToString());
            BonSai.ProductsDataTable productDT = new ProductsTableAdapter().GetProductByProductID(productID);
            if (productDT.Rows.Count > 0) 
            {
                BonSai.ProductsRow productRow = productDT.Rows[0] as BonSai.ProductsRow;
                if (!productRow.IsRepresentImageNull()) 
                {
                    imgProductRepresent.ImageUrl = "~/img_product/" + productRow.RepresentImage;
                }

                if (!productRow.IsProductNameNull())
                {
                    lblProductName.Text = productRow.ProductName;
                }
                else 
                {
                    Response.Redirect("ProductManage.aspx");
                }
            }
             
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
       
        if (RadUpload1.InvalidFiles.Count > 0)
        {
            lblImageType.Text = "Bạn chỉ được nhập loại  file .JPG & .PNG !";
            return;
        }

        if (ViewState["productID"] != null)
        {
            int productID = Convert.ToInt32(ViewState["productID"].ToString());


            ///THEM FILE MOI
            ProductsTableAdapter productDA = new ProductsTableAdapter();
            string representImage = null;

            if (RadUpload1.UploadedFiles.Count > 0)
            {
                foreach (UploadedFile validFile in RadUpload1.UploadedFiles)
                {
                    System.IO.FileInfo f = new System.IO.FileInfo(RadUpload1.UploadedFiles[0].FileName);
                    String oldFileName = f.Name;
                    String newFileName = Server.MapPath(".") + "\\..\\img_product\\" + productID + f.Extension;
                    if (System.IO.File.Exists(newFileName))
                        System.IO.File.Delete(newFileName);

                    System.IO.File.Move(Server.MapPath(".") + "\\..\\img_product\\" + f.Name, newFileName);
                    representImage = productID + f.Extension;
                }
            }
            int i = productDA.UpdateRepresentImageByProductID(representImage, productID);
            Response.Redirect("ProductDetails.aspx?productID=" + productID + "&code=image");
        }
        else
            return;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (ViewState["productID"] != null)
        {
            int productID = Convert.ToInt32(ViewState["productID"].ToString());
            Response.Redirect("ProductDetails.aspx?productID=" + productID);
        }
    }
}
